package cn.lyf.tools;

import java.nio.file.*;
import java.util.*;

class Tuple<T, N> {
    T val;
    N next;
    public Tuple(){

    }
    public Tuple(T val, N next) {
        this.val = val;
        this.next = next;
    }
}

public class App {
    public static void main(String[] args) {
        Path[] paths = new Path[] { Paths.get("C:/Users/lyf/Desktop/TinyJava/tiny-rigger/linux-tool/a.txt"),
                Paths.get("C:/Users/lyf/Desktop/TinyJava/tiny-rigger/linux-tool/b.txt"),
                Paths.get("C:/Users/lyf/Desktop/TinyJava/tiny-rigger/linux-tool/c.txt") };
        AWK awk = new AWK();
        final HashMap<String, Tuple<Integer, String>> a = new HashMap<>();
        awk.addStatement(ctx -> {
            List<String> $ = ctx.$;
            String FS = ctx.FS;
            String k = $.get(1) + FS + $.get(2) + FS + $.get(3);
            Tuple<Integer, String> rd = a.getOrDefault(k, new Tuple<>(0,""));
            rd.val+=Integer.valueOf($.get(4));
            rd.next=rd.next.equals("")?$.get(5):rd.next+";"+$.get(5);
            a.put(k, rd);
        });
        
        awk.END=ctx->{
            a.entrySet().stream().forEach(x->{
                System.out.printf("%-25s %-7s %-13s\n",x.getKey(),x.getValue().val,x.getValue().next);
            });
        };


        awk.setFiles(paths);
        awk.doScript();

    }
}
